/** 
 * File-Name:ArticleAction.java
 *
 * Created on 2011-6-2 下午10:10:52
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.web;

import java.io.File;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import neoutil.CommonAction;
import neoutil.CommonService;
import neoutil.Config;
import neoutil.FileUtils;
import neoutil.Page;

import org.apache.log4j.Logger;

import site.entity.Article;
import site.entity.Category;
import site.entity.User;
import site.helper.CommonHelper;
import site.helper.TableHeader;
import site.helper.TableHeaderFactory;
import site.service.ArticleRepository;
import site.service.CategoryRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午10:10:52
 */
@SuppressWarnings("serial")
public class ArticleAction extends CommonAction {
	private static Logger log = Logger.getLogger(ArticleAction.class);
	private Article article;
	private File articleImage;
	private String articleImageContentType;
	private String articleImageFileName;
	private File articleFile;
	private String articleFileContentType;
	private String articleFileFileName;
	private Page<Article> page = new Page<Article>(15);
	private int cid;
	private Category category;
	private List<Category> categories;
	private List<Article> hotArticls = null;
	private List<TableHeader> tableHeaders = null;

	public String toAddArticle() {
		try {
			cid = Integer.parseInt(getRequestParameter("cid"));
			category = getCategoryRepository().getCategoryByCid(cid);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String addArticle() {
		try {
			article.setAuthor((User) getSessionObject("loginUser"));

			if (articleFile != null) {
				String fileName = article.getUuid()
						+ FileUtils.getExtention(getArticleFileFileName());
				FileUtils.copy(getArticleFile(),
						new File(Config.getArticleSavePath() + fileName));

				article.setFilePath(fileName);
			}

			if (CommonService.SUCCESS == getArticleRepository().save(article)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String downloadArticleEnclosure() {
		String uuid = getRequestParameter("uuid");
		article = getArticleRepository().get(uuid);

		if (article != null
				&& article.getFilePath() != null
				&& new File(Config.getArticleSavePath() + article.getFilePath())
						.exists()) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public InputStream getEnclosureFile() {
		try {
			String uuid = getRequestParameter("uuid");
			article = getArticleRepository().get(uuid);
			
			String URL = Config.getArticleSavePath() + article.getFilePath();
			article.setFilePath(article.getTitle() + "." + CommonHelper.getExtension(URL));
			article.setFilePath(URLEncoder.encode(article.getFilePath(),"UTF-8"));
			
			return new java.io.FileInputStream(URL);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String toEditArticle() {
		try {
			String uuid = getRequestParameter("uuid");
			if (uuid == null || "".equals(uuid)) {
				return ERROR;
			} else {
				article = getArticleRepository().get(uuid);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String editArticle() {
		try {
			if (articleFile != null) {
				String fileName = article.getUuid()
						+ FileUtils.getExtention(getArticleFileFileName());
				FileUtils.copy(getArticleFile(),
						new File(Config.getArticleSavePath() + fileName));

				article.setFilePath(fileName);
			}

			article.setAuthor((User) getSessionObject("loginUser"));
			if (CommonService.SUCCESS == getArticleRepository().update(article)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String deleteArticle() {
		try {
			String uuid = getRequestParameter("uuid");
			if (CommonService.SUCCESS == getArticleRepository()
					.deleteArticleByUuid(uuid)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String getArticleByUuid() {
		try {
			String uuid = getRequestParameter("uuid");
			if (uuid == null || "".equals(uuid)) {
				return ERROR;
			} else {
				ArticleRepository articleRepository = getArticleRepository();
				article = articleRepository.get(uuid);

				// 更新访问次数
				if (article != null) {
					article.setVisitNum(article.getVisitNum() + 1);
				}
				articleRepository.updateVisitNum(article);
				// 查询概要热点新闻

				CategoryRepository categoryRepository = getCategoryRepository();
				category = categoryRepository
						.getCategoryByCid(article.getCid());
				categories = categoryRepository
						.getCategoriesByParentCid(category.getParentCid());

				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String getArticlesByCid() {
		try {
			cid = Integer.parseInt(getRequestParameter("cid"));
			ArticleRepository articleRepository = getArticleRepository();
			page = articleRepository.getCheckedArticlesByCid(page, cid);

			CategoryRepository categoryRepository = getCategoryRepository();
			category = categoryRepository.getCategoryByCid(cid);
			categories = categoryRepository.getCategoriesByParentCid(category
					.getParentCid());
			
			tableHeaders = TableHeaderFactory.getArticleTableHeaders();

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String getLatestArticleByCid() {
		try {
			int cid = Integer.parseInt(getRequestParameter("cid"));
			ArticleRepository articleRepository = getArticleRepository();
			article = articleRepository.getLatestArticleByCid(cid);
			if (article != null) {
				article.setVisitNum(article.getVisitNum() + 1);
				articleRepository.updateVisitNum(article);
			}

			CategoryRepository categoryRepository = getCategoryRepository();
			category = categoryRepository.getCategoryByCid(cid);
			categories = categoryRepository.getCategoriesByParentCid(category
					.getParentCid());

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String searchArticles() {
		try {
			String query = getRequestParameter("query");
			if (query == null || "".equals(query)) {
				return INPUT;
			} else {
				System.err.println("Query:"
						+ new String(query.getBytes("iso-8859-1"), "UTF-8"));
				ArticleRepository articleRepository = getArticleRepository();
				page = articleRepository.search(page,
						new String(query.getBytes("iso-8859-1"), "UTF-8"));
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public String manageArticles() {
		cid = Integer.parseInt(getRequestParameter("cid"));
		page = getArticleRepository().getArticlesByCid(page, cid);
		category = getCategoryRepository().getCategoryByCid(cid);
		return SUCCESS;
	}

	// Getters and Setters
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Page<Article> getPage() {
		return page;
	}

	public void setPage(Page<Article> page) {
		this.page = page;
	}

	public File getArticleImage() {
		return articleImage;
	}

	public void setArticleImage(File articleImage) {
		this.articleImage = articleImage;
	}

	public String getArticleImageContentType() {
		return articleImageContentType;
	}

	public void setArticleImageContentType(String articleImageContentType) {
		this.articleImageContentType = articleImageContentType;
	}

	public String getArticleImageFileName() {
		return articleImageFileName;
	}

	public void setArticleImageFileName(String articleImageFileName) {
		this.articleImageFileName = articleImageFileName;
	}

	public File getArticleFile() {
		return articleFile;
	}

	public void setArticleFile(File articleFile) {
		this.articleFile = articleFile;
	}

	public String getArticleFileContentType() {
		return articleFileContentType;
	}

	public void setArticleFileContentType(String articleFileContentType) {
		this.articleFileContentType = articleFileContentType;
	}

	public String getArticleFileFileName() {
		return articleFileFileName;
	}

	public void setArticleFileFileName(String articleFileFileName) {
		this.articleFileFileName = articleFileFileName;
	}

	public List<Article> getHotArticls() {
		return hotArticls;
	}

	public void setHotArticls(List<Article> hotArticls) {
		this.hotArticls = hotArticls;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public void setTableHeaders(List<TableHeader> tableHeaders) {
		this.tableHeaders = tableHeaders;
	}

	public List<TableHeader> getTableHeaders() {
		return tableHeaders;
	}
}
